package com.tybbank.Login;

import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.YZMBean;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.CountDownButtonHelper;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.StatusBarUtil;
import com.tybbank.Utils.YanZhengUtil;
import com.tybbank.Utils.logUtilssss;
import com.tybbank.web.WebActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/26 0026.
 */

public class RegisterVipActivity extends BaseActivity {
    @BindView(R.id.et_yaoqingma)
    EditText etYaoqingma;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.bt_yanzhengma)
    Button btYanzhengma;
    @BindView(R.id.bt_enter)
    Button btEnter;
    @BindView(R.id.ck_checkbox)
    CheckBox ckCheckbox;
    @BindView(R.id.text_wuyong)
    TextView textWuyong;
    @BindView(R.id.tv_xieyi)
    TextView tvXieyi;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.ib_exit)
    ImageButton ibExit;
    @BindView(R.id.tv_guonei)
    TextView tvGuonei;
    @BindView(R.id.line_guonei)
    View lineGuonei;
    @BindView(R.id.rl_guonei)
    RelativeLayout rlGuonei;
    @BindView(R.id.tv_guowai)
    TextView tvGuowai;
    @BindView(R.id.line_guowai)
    View lineGuowai;
    @BindView(R.id.rl_guowai)
    RelativeLayout rlGuowai;
    @BindView(R.id.ll_gone_yzm)
    LinearLayout llGoneYzm;
    private int tag = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_vip);
        ButterKnife.bind(this);
        tag = 1;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            StatusBarUtil.setStatusBarColor(RegisterVipActivity.this, R.color.backgroundcolorhei723);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    @Override
    public void LeftClose() {
        super.LeftClose();
        RegisterVipActivity.this.finish();
    }

    @OnClick({R.id.bt_yanzhengma, R.id.bt_enter, R.id.tv_xieyi, R.id.ib_exit, R.id.rl_guonei, R.id.rl_guowai})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_exit://退出键
                RegisterVipActivity.this.finish();
                break;
            case R.id.rl_guonei:    //国内
                tag = 1;
                llGoneYzm.setVisibility(View.VISIBLE);
                tvGuonei.setTextColor(getResources().getColor(R.color.textcolormain));
                lineGuonei.setBackgroundColor(getResources().getColor(R.color.textcolormain));
                tvGuowai.setTextColor(getResources().getColor(R.color.textcolorbai));
                lineGuowai.setBackgroundColor(getResources().getColor(R.color.textcolorbai));
                break;
            case R.id.rl_guowai:    //国外
                tag = 2;
                llGoneYzm.setVisibility(View.GONE);
                tvGuowai.setTextColor(getResources().getColor(R.color.textcolormain));
                lineGuowai.setBackgroundColor(getResources().getColor(R.color.textcolormain));
                tvGuonei.setTextColor(getResources().getColor(R.color.textcolorbai));
                lineGuonei.setBackgroundColor(getResources().getColor(R.color.textcolorbai));
                break;
            case R.id.tv_xieyi: //协议
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra("title", getString(R.string.fuwutiaokuan));
              //  intent.putExtra("url", Constant.KEFU);
               intent.putExtra("url", Constant.xieyi);
                startActivity(intent);
                break;
            case R.id.bt_yanzhengma:
                if (etPhone.getText().toString().trim().length() == 0) {
                    Toast.makeText(getBaseContext(), getString(R.string.yourphone), Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    String time = MD5Utils.getTime();
                    SortedMap<String, String> parameters = new TreeMap<String, String>(
                            new Comparator<String>() {
                                public int compare(String obj1, String obj2) {
                                    // 降序排序
                                    return obj1.compareTo(obj2);
                                }
                            });
                    parameters.put("phone", etPhone.getText().toString().trim());
                    parameters.put("TimeStamp", time);
                    parameters.put("type", "1");
                    String sign = MD5Utils.createSign(parameters);
                    getokhttp(time, sign);
                }
                break;
            case R.id.bt_enter:
                if (etPhone.getText().toString().trim().length() == 0) {
                    Toast.makeText(getBaseContext(), getString(R.string.yourphone), Toast.LENGTH_SHORT).show();
                    return;
                } else if (etPwd.getText().toString().trim().length() < 6 || etPwd.getText().toString().trim().length() > 32) {
                    Toast.makeText(getBaseContext(), getString(R.string.pwdhint), Toast.LENGTH_SHORT).show();
                    return;
                } else if (etPayPwd.getText().toString().trim().length() != 6) {
                    Toast.makeText(getBaseContext(), getString(R.string.paypwdhint), Toast.LENGTH_SHORT).show();
                    return;
                } else if (etYaoqingma.getText().toString().length() == 0) {
                    logUtilssss.toast(this, getString(R.string.sryqm));
                    return;
                } else if (!ckCheckbox.isChecked()) {
                    showTips(getString(R.string.xieyitoast));
                    return;
                } else {
                    if (YanZhengUtil.isLetterDigit(etPwd.getText().toString().trim()) == true) {

                        if (tag==1){    // 1 是国内  2 是国外
                            if (etYzm.getText().toString().length() == 0) {
                                logUtilssss.toast(this, getString(R.string.sryzm));
                                return;
                            }
                            String time = MD5Utils.getTime();
                            SortedMap<String, String> parameters = new TreeMap<String, String>(
                                    new Comparator<String>() {
                                        public int compare(String obj1, String obj2) {
                                            // 降序排序
                                            return obj1.compareTo(obj2);
                                        }
                                    });
                            parameters.put("account", etPhone.getText().toString().trim());
                            parameters.put("referee", etYaoqingma.getText().toString().trim());
                            parameters.put("verify_code", etYzm.getText().toString().trim());
                            parameters.put("login_pwd", MD5Utils.md5Password(etPwd.getText().toString().trim()));
                            parameters.put("pay_pwd", MD5Utils.md5Password(etPayPwd.getText().toString().trim()));
                            parameters.put("TimeStamp", time);
                            String sign = MD5Utils.createSign(parameters);
                            inithttp(time, sign);//接口

                        }else {
                            String time = MD5Utils.getTime();
                            SortedMap<String, String> parameters = new TreeMap<String, String>(
                                    new Comparator<String>() {
                                        public int compare(String obj1, String obj2) {
                                            // 降序排序
                                            return obj1.compareTo(obj2);
                                        }
                                    });
                            parameters.put("account", etPhone.getText().toString().trim());
                            parameters.put("referee", etYaoqingma.getText().toString().trim());
                            parameters.put("login_pwd", MD5Utils.md5Password(etPwd.getText().toString().trim()));
                            parameters.put("pay_pwd", MD5Utils.md5Password(etPayPwd.getText().toString().trim()));
                            parameters.put("TimeStamp", time);
                            String sign = MD5Utils.createSign(parameters);
                            inithttpttt(time, sign);//接口
                        }

                    } else {
                        Toast.makeText(getBaseContext(), getString(R.string.pwdhint), Toast.LENGTH_SHORT).show();
                        return;

                    }

                }

                break;
        }
    }
    private void inithttpttt(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.abroad_register)
                .addParams("account", etPhone.getText().toString().trim())
                .addParams("referee", etYaoqingma.getText().toString().trim())
                .addParams("login_pwd", MD5Utils.md5Password(etPwd.getText().toString().trim()))
                .addParams("pay_pwd", MD5Utils.md5Password(etPayPwd.getText().toString().trim()))
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG会员注册" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        try {
                            YZMBean enterBean = new Gson().fromJson(json, new TypeToken<YZMBean>() {
                            }.getType());
                            int status = enterBean.getCode();
                            if (status == 1011) {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                                RegisterVipActivity.this.finish();
                            } else {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {

                        }
                    }
                });
    }
    private void inithttp(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.REGISTERVIP)
                .addParams("account", etPhone.getText().toString().trim())
                .addParams("referee", etYaoqingma.getText().toString().trim())
                .addParams("verify_code", etYzm.getText().toString().trim())
                .addParams("login_pwd", MD5Utils.md5Password(etPwd.getText().toString().trim()))
                .addParams("pay_pwd", MD5Utils.md5Password(etPayPwd.getText().toString().trim()))
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG会员注册" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        try {
                            YZMBean enterBean = new Gson().fromJson(json, new TypeToken<YZMBean>() {
                            }.getType());
                            int status = enterBean.getCode();
                            if (status == 1011) {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                                RegisterVipActivity.this.finish();
                            } else {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {

                        }
                    }
                });
    }

    //   短信接口
    private void getokhttp(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.SMS)
                .addParams("phone", etPhone.getText().toString().trim())
                .addParams("type", "1")
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG短信接口" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        YZMBean enterBean = new Gson().fromJson(json, new TypeToken<YZMBean>() {
                        }.getType());
                        int status = enterBean.getCode();
                        if (status == 1011) {
                            //    //短信请求的的代码
                            CountDownButtonHelper helper = new CountDownButtonHelper(btYanzhengma,
                                    getString(R.string.sent), 60, 1);
                            helper.setOnFinishListener(new CountDownButtonHelper.OnFinishListener() {
                                @Override
                                public void finish() {
                                    btYanzhengma.setText(getString(R.string.hqyzm));
                                }
                            });
                            helper.start();
                            Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }


                    }
                });
    }

}
